package com.topscomm.mpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.MpmScript;
import com.topscomm.mpm.pojo.MpmProjectAppointmentEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.impl.MpmProjectAppointmentService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class MpmAppointmentSignerApprovalScript extends MpmScript {

    /**
     * 签发人审批后需要生成任命书编号
     *
     * @param service
     * @param entity
     * @throws Exception
     */
    @Transactional
    public void afterSignerApproval(ITapService service, TapEntity entity) throws Exception {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
        MpmProjectAppointmentService mpmProjectAppointmentService = (MpmProjectAppointmentService) SpringUtil.getBean("mpmProjectAppointmentService");
        MpmProjectService mpmProjectService = (MpmProjectService) SpringUtil.getBean("mpmProjectService");

        // 获取任命书信息
        MpmProjectAppointmentEntity mpmProjectAppointmentEntity = mpmProjectAppointmentService.queryById(sourceid);
        if (mpmProjectAppointmentService.judgeSendTime(null, mpmProjectAppointmentEntity.getCreateon())) {
            return;
        }
        String projectCode = mpmProjectAppointmentEntity.getProjectcode();
        String sysPush = mpmProjectAppointmentEntity.getSyspush();
        String appointNumber = "";
        if (!StringUtil.isEmpty(sysPush) && sysPush.equals("1") && StringUtil.isEmpty(mpmProjectAppointmentEntity.getAppointmentnumber())) {
            // 首次任命
            appointNumber = mpmProjectAppointmentService.generateAppointNumber(projectCode);
        }
        else {
            // 二次任命
            StringBufferProxy sql = new StringBufferProxy();
            sql.appendSingle(" {0} = '{1}' ", MpmProjectAppointmentEntity.FieldProjectcode, projectCode);
            sql.appendSingle(" and {0} = '{1}'", MpmProjectAppointmentEntity.FieldSyspush, "1");
            List<MpmProjectAppointmentEntity> mpmProjectAppointmentEntities = mpmProjectAppointmentService.queryByWhere(sql.toString());
            if (!CollectionUtils.isEmpty(mpmProjectAppointmentEntities)) {
                MpmProjectAppointmentEntity firstAppointment = mpmProjectAppointmentEntities.get(0);
                appointNumber = firstAppointment.getAppointmentnumber();
            }
            if (StringUtil.isEmpty(appointNumber)) {
                appointNumber = mpmProjectAppointmentService.generateAppointNumber(projectCode);
            }
        }
        mpmProjectAppointmentEntity.setAppointmentnumber(appointNumber);

        // 更新任命书编号信息
        mpmProjectAppointmentService.updateEntity(mpmProjectAppointmentEntity);

        List<MpmProjectEntity> mpmProjectDoList = mpmProjectService.queryByWhere(" projectcode = '" + projectCode + "'");
        if (CollectionUtils.isEmpty(mpmProjectDoList)) {
            return;
        }
        // 重新生成任命书
        mpmProjectAppointmentService.generateAppointment(mpmProjectAppointmentEntity, mpmProjectDoList.get(0));
    }
}
